<?php

    class Admin_Model_NewMapper {
        public function _insert($title, $content, $img) {
            $data = array(
                'title' => $title,
                'content' => $content,
                'date' => new Zend_Db_Expr('now()'),
                'img' => $date
            );
            $table = new Application_Model_DbTable_New();
            $table->insert($data);

        }

        public function _getAll() {
            $link = mysql_connect('localhost', 'root', '');
            mysql_select_db('aitishop', $link);
            $query = "select * from news";
            $result = mysql_query($query);
            $arr = array();
            while ($row = mysql_fetch_array($result)) {
                $new = new Admin_Model_New($row['id'], $row['title'], $row['content'], $row['date'], $row['img']);
                $arr[] = $new;
            }
            return $arr;
        }

        public function _getById($id) {
            $table = new Application_Model_DbTable_New();
            $row = $table->fetchRow('id = ' . $id);
            if ($row) {
                $new = new Admin_Model_New($row->id, $row->title, $row->content, $row->date, $row->img);
                return $new;
            }
        }

        public function _delete($id) {
            $table = new Application_Model_DbTable_New();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->delete();
            }
        }

        public function _update($id, $title, $content, $img) {
            $table = new Application_Model_DbTable_New();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->title = $title;
                $row->content = $content;
                $row->img = $img;
                $row->save();
            }
        }
function getExtention($fileName){
 	$posdot=strrpos($fileName,'.');
	$ext=substr($fileName,$posdot+1);
	return strtolower($ext);
 }
 
 function uploadFile($files,$url,$allowType,$size){
 	$fileName=isset($files['name'])?$files['name']:'';
	$tmpFile=isset($files['tmp_name'])?$files['tmp_name']:'';
	$fileSize=isset($files['size'])?$files['size']:'';
	$ext=getExtention($fileName);
	if(!in_array($ext,$allowType)){
		return -1;
	}
	if($size<$fileSize){
		return -2;
	}
	$fileName=time().'_'.$fileName;
	if(move_uploaded_file($tmpFile,$url.$fileName)){
		chmod($url.$fileName,677);
		return $fileName;
	}
 }
    }
